<!--
  Copyright 2018 The Outline Authors

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<link rel='import' href='../bower_components/polymer/polymer.html'>
<link rel='import' href='../bower_components/paper-button/paper-button.html'>
<link rel='import' href='../bower_components/paper-input/paper-input.html'>
<link rel='import' href='../bower_components/paper-toggle-button/paper-toggle-button.html'>

<link rel='import' href='./cloud-install-styles.html'>
<link rel='import' href='./outline-iconset.html'>

<dom-module id='outline-server-settings'>
  <template>
    <style include='cloud-install-styles'></style>
    <style>
      .fullScreenOverlay {
        height: 100vh;
        width: 100vw;
        position: fixed;
        top: 0;
        z-index: 101;  /* must be higher than the outline-help-bubble */
        background: white;
      }
      app-toolbar iron-icon {
        margin-right: 24px;
      }
      .content {
        flex-grow: 1;
      }
      .setting {
        padding: 24px;
        display: flex;
        align-items: flex-start;
      }
      .setting:not(:first-child) {
        border-top: 1px solid rgba(0, 0, 0, 0.08);
      }
      .setting > iron-icon {
        margin-right: 24px;
        width: 40px;
        color: #000;
        opacity: 0.87;
      }
      .setting > div {
        width: 100%;
      }
      .setting h3 {
        margin: 0 0 16px 0;
        padding: 0;
        color: #000;
        opacity: 0.87;
        font-size: 18px;
        font-weight: 500;
      }
      .setting p {
        margin-bottom: 12px;
      }
      #metricsHeader {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
      }
      .detail {
        margin-top: 0px;
        font-size: 12px;
      }
      .clickable {
        cursor: pointer;
      }
      paper-toggle-button {
        --paper-toggle-button-checked-bar-color: var(--primary-green);
        --paper-toggle-button-checked-button-color: var(--primary-green);
        --paper-toggle-button-checked-ink-color: var(--primary-green);
      }
      paper-input {
        /* Removes extra padding added by children of paper-input */
        margin-top: -8px;
        --paper-input-container-label-focus: {
          color: var(--primary-green);
        };
        --paper-input-container-underline-focus: {
          border-color: var(--primary-green);
        };
        --paper-input-container-label: {
          font-size: 14px;
          line-height: 22px;
        };
      }
      paper-input[disabled] {
        --paper-input-container-underline: {
          display: none;
        };
        --paper-input-container-underline-focus: {
          display: none;
        };
        --paper-input-container-underline-disabled: {
          display: none;
        };
        --paper-input-container-disabled: {
          opacity: 1;
        }
      }
    </style>
    <div class='fullScreenOverlay' hidden$='[[!visible]]'>
      <app-toolbar>
        <paper-icon-button icon="arrow-back" on-tap="save"></paper-icon-button>
        <h2>Server Details</h2>
      </app-toolbar>
      <div class='content'>
        <div class='setting'>
          <iron-icon icon="outline-iconset:outline"></iron-icon>
          <div>
            <h3>Information</h3>
            <paper-input value='{{name}}' label='Name' always-float-label maxlength='100'></paper-input>
            <p class='detail'>Set a new name for your server. Note that this will not be reflected on the devices of the users that you invited to connect to it.</p>
            <paper-input disabled value='[[serverHostname]]' label='Hostname' hidden$='[[!serverHostname]]' always-float-label maxlength='100'></paper-input>
            <paper-input disabled value='[[serverManagementPort]]' label='Management Port' hidden$='[[!serverManagementPort]]' always-float-label maxlength='100'></paper-input>
            <paper-input disabled value='[[serverCreationDate]]' label='Created' hidden$='[[!serverCreationDate]]' always-float-label maxlength='100'></paper-input>
            <paper-input disabled value='[[serverId]]' label='Server ID' hidden$='[[!serverId]]' always-float-label maxlength='100'></paper-input>
          </div>
        </div>
        <!-- Metrics controls -->
        <div class='setting'>
          <iron-icon icon="editor:insert-chart"></iron-icon>
          <div>
            <div id='metricsHeader'>
              <h3>Share anonymous metrics</h3>
              <paper-toggle-button checked='{{metricsEnabled}}'></paper-toggle-button>
            </div>
            <p>Share anonymized metrics to help improve the reliability and performance of Outline, for you and for those you share your server with. <a class="learnMoreLink" href="https://s3.amazonaws.com/outline-vpn/index.html#/en/support/dataCollection">Learn more</a>.</p>
          </div>
        </div>
      </div>
    </div>
  </template>
  <script>
    Polymer({
      is: 'outline-server-settings',
      properties: {
        deleteEnabled: Boolean,
        forgetEnabled: Boolean,
        name: String,
        metricsEnabled: Boolean,
        visible: {type: Boolean, value: false},
        // Initialize to null so we can use the hidden attribute, which does not work well with undefined values.
        serverId: {type: String, value: null},
        serverHostname: {type: String, value: null},
        serverManagementPort: {type: Number, value: null},
        serverCreationDate: {type: String, value: null}
      },
      open: function(name, metricsEnabled) {
        this.visible = true;
        this.initialName = name;
        this.name = name;
        this.initialMetricsEnabled = metricsEnabled;
        this.metricsEnabled = metricsEnabled;
      },
      close: function() {
        this.visible = false;
      },
      save: function() {
        // Fire signal if name has changed.
        if (this.name !== this.initialName) {
          const DEFAULT_SERVER_NAME = 'Outline Server';
          const newName = this.name || DEFAULT_SERVER_NAME;
          this.fire('ServerRenameRequested', {newName});
        }
        // Fire signal if metrics have changed.
        if (this.metricsEnabled !== this.initialMetricsEnabled) {
          const metricsSignal = this.metricsEnabled ?
              'EnableMetricsRequested' : 'DisableMetricsRequested';
          this.fire(metricsSignal);
        }
        this.close();
      }
    });
  </script>
</dom-module>
